package org.easy.eao.spring.jpa;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import javax.persistence.EntityManagerFactory;

import org.easy.eao.Action;
import org.easy.eao.utils.ReflectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.jpa.JpaTemplate;

/**
 * JPA 存取动作支持类
 * @author Frank
 */
@SuppressWarnings("unchecked")
public abstract class JpaActionSupport<A extends Annotation> 
	implements Action<A> {

	protected A annotation;
	protected Method method;
	protected Class<?> clz;

	@Override
	public void initialize(Annotation annotation, Method method, Class<?> clz) {
		this.annotation = (A) annotation;
		this.method = method;
		this.clz = clz;
	}

	@Override
	public Class<A> getAnnotationClass() {
		return (Class<A>) ReflectUtils.getActualGenericType(this.getClass(), JpaActionSupport.class, "A");
	}

	private JpaTemplate jpaTemplate;

	@Autowired
	public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
		this.jpaTemplate = new JpaTemplate(entityManagerFactory);
	}

	public final JpaTemplate getJpaTemplate() {
		return this.jpaTemplate;
	}

}